package com.atguigu.controller;

import com.atguigu.pojo.Category;
import com.atguigu.service.CategoryService;
import com.atguigu.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program:weiblog-server
 * @author: 马雨清
 * @Time: 2023/8/20  21:59
 * @description:
 */

@RestController
@RequestMapping("/category")
@CrossOrigin
@Tag(name = "CategoryController")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "查找所有类型")
    @GetMapping("/findAll")
    public Result<List<Category>> findAllCate(){
        List<Category> categoryList = categoryService.findAllCategory();
        return Result.ok(categoryList);
    }

    @Operation(summary = "添加分类")
    @PostMapping("insertCategory")
    public Result addCategory(@RequestBody(required = false) Category category){
        Integer result = categoryService.addCategory(category);
        if (result==0){
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "条件分页查询")
    @PostMapping("findPage/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable Integer pageNum,
                             @PathVariable Integer pageSize,
                             @RequestBody Category category){
        Map<String,Object> map = categoryService.findByPage(pageNum,pageSize,category);
        return Result.ok(map);
    }

}
